package com.code.demo.ui.guide;

import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.code.demo.R;


/**
 * Created by yangliguo on 2018/3/11.
 */

public class GuidePagerAdapter extends PagerAdapter {

    public static final int[] GUIDE_PAGES = {R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3};

    private boolean isMultiScr;
    private Handler mHandler;

    public GuidePagerAdapter(boolean isMultiScr) {
        this.isMultiScr = isMultiScr;
    }

    public void setData(Handler handler) {
        this.mHandler = handler;
    }

    @Override
    public int getCount() {
        return GUIDE_PAGES.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.guide_page, container, false);

        ImageView image = (ImageView) view.findViewById(R.id.image);
        image.setImageResource(GUIDE_PAGES[position]);

        TextView btn = (TextView) view.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.sendEmptyMessage(0);
            }
        });

        if (position == GUIDE_PAGES.length - 1) {
            btn.setVisibility(View.VISIBLE);
        } else {
            btn.setVisibility(View.GONE);
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
